package base

/*
* @author: hjz
* @time: 2025/4/26 18:08
* @ 注意要从最小的数字开始去遍历整个map
 */
func longestConsecutive(nums []int) int {
	numSet := make(map[int]struct{}, len(nums))
	for _, num := range nums {
		numSet[num] = struct{}{}
	}
	res := 0
	for num := range numSet {
		// 只有当num是序列的起点时才进行计算
		if _, exists := numSet[num-1]; !exists {
			currentNum := num
			tmp := 1
			for _, exists = numSet[currentNum+1]; exists; _, exists = numSet[currentNum+1] {
				currentNum++
				tmp++
			}

			if tmp > res {
				res = tmp
			}
		}
	}

	return res
}
func longestConsecutive1(nums []int) int {
	m := make(map[int]struct{}, len(nums))
	for _, num := range nums {
		m[num] = struct{}{}
	}
	res := 0
	for _, num := range nums {
		_, ok := m[num+1] // 这样子是认为每个数字都是起点然后去遍历整个map,会超时
		i := 1
		for ok {
			i++
			num++
			_, ok = m[num+1]
		}
		if res < i {
			res = i
		}
	}
	return res
}
